package coms.process;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import com.beans.AdminUserBean;
import com.beans.CategoryBean;
import com.beans.ClaimBean;
import com.beans.ClaimHeadInfoBaseBean;
import com.beans.FormBean;
import com.beans.ORGBean;
import com.beans.ProjectBean;
import com.beans.SubCategory;
import com.beans.VendorBean;
import com.crc.mam.BaseCase;
import coms.AutoException;
import coms.Constants;
import frame.FrameWork;
import frame.InterTest;
import functions.ems_base_data.Category;
import functions.ems_base_data.Project;
import functions.ems_base_data.Vendor;
import functions.ems_claim.Claim;
import functions.ems_claim.ClaimLine;
import functions.ems_claim.ClaimPay;
import functions.ems_claim.ClaimProcess;
import functions.ems_process_service.Psc;
import functions.mams_rest.Org;
import functions.mams_rest.User;

/**
 * 供应商预付款报账单申请
 * @author CHENHAILIAN6
 *
 */
public class VendorLoanClaimProcess extends BaseCase implements InterTest {
	
	String claimId;
	String claimNo;
	String amount;
	ProjectBean project;
	FormBean form;
	String result;
	AdminUserBean loginUserBean;
	String categoryName;
	boolean isPassed = false;
	String categoryCode ;
	String applyCompany;
    ORGBean applyOrgBean;
    
	public String getCategoryName() {
    return categoryName;
  }

  public void setCategoryName(String categoryName) {
    this.categoryName = categoryName;
  }

  public String getCategoryCode() {
    return categoryCode;
  }

  public void setCategoryCode(String categoryCode) {
    this.categoryCode = categoryCode;
  }

  public String getAmount() {
    return amount;
  }

  public void setAmount(String amount) {
    this.amount = amount;
  }

  public String getClaimId() {
		return claimId;
	}

	public void setClaimId(String claimId) {
		this.claimId = claimId;
	}

	public String getClaimNo() {
		return claimNo;
	}

	public void setClaimNo(String claimNo) {
		this.claimNo = claimNo;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public boolean isPassed() {
		return isPassed;
	}

	public void setPassed(boolean isPassed) {
		this.isPassed = isPassed;
	}

	public String getApplyCompany() {
    return applyCompany;
  }

  public void setApplyCompany(String applyCompany) {
    this.applyCompany = applyCompany;
  }

  public ORGBean getApplyOrgBean() {
    return applyOrgBean;
  }

  public void setApplyOrgBean(ORGBean applyOrgBean) {
    this.applyOrgBean = applyOrgBean;
  }



	@Override
	public void run(Map<Object, Object> paramMap) {
	  String baseUrl = (String) paramMap.get("baseUrl");
      if(baseUrl !=null) {
        FrameWork.setBaseUrl(baseUrl);
      }
      LoginTest loginTest = new LoginTest();
      loginTest.run(paramMap);
      if(!loginTest.status) {
        remark = loginTest.result;
        status = false;
        throw new AutoException("登录失败，阻碍测试");
      }
	  /**
       * 
       * 员工借款/还款4001001
       */
      Psc psc = new Psc();
      CategoryBean categoryBean = null ;
      categoryCode = (String) paramMap.get("categoryCode");
      categoryName = (String) paramMap.get("categoryName");
      if(categoryCode == null) {
        Category category = new Category();
        categoryBean = category.getCategoryByName(categoryName);
        categoryCode = categoryBean.getCategoryCode();
        if(categoryBean == null) {
          throw new AutoException("找不到对应的业务大类：" +categoryName+ ", 请确认后重新提交");
        }
      }
      psc.selectFlowParam(categoryCode);
      FormBean form = psc.getForm();
      User user = new User();
      user.admin();
      AdminUserBean bean = user.getUserBean();
      
      Claim claim = new Claim(bean,categoryCode,categoryName);
      Vendor vendor = new Vendor();
              
      vendor.findPagedVendorList("VENDOR");
      VendorBean vendorBean = vendor.getVendor();

      claim.gongyingshangSaveClaimHeadAndLine(vendorBean);
      if(!claim.status) {
        status = false;
        remark = claim.getResult();
        throw new AutoException("表单头保存失败，无法继续进行测试");
      }
      Org org = new Org();
      applyCompany = bean.getCompanyDesc();
      applyOrgBean = org.getRandomOrg(applyCompany, bean.getDepartment());
      claim.getClaimHeadAndLine();
      ClaimBean responseClaimBean = claim.getClaimHeadResBean();
      claimId = claim.getClaimId();
      System.out.println("claimId ------- " + claimId);
      
      Project project = new Project();
      project.findPagedProjectList(bean.getFinanceCompany());
      ProjectBean selectProject = project.getSelectProject();

      
      ClaimLine line = new ClaimLine();
      Category category = new Category();
      category.subcategory(categoryCode);
      ArrayList<SubCategory> subCategories = category.getSubCategories();
      SubCategory subCategory = subCategories.get(0);
      line.insert(claimId);
      if(!line.isPassed()){
        status = false;
        remark = line.getResult();
        throw new AutoException("插入报账明细失败，无法继续进行测试");
      }
      amount  = line.getAmount();
      line.findList(claimId);
      ClaimPay pay = new ClaimPay();
      pay.claimPayPlanInser(claimId);
      if(!pay.status){
        status = false;
        remark = pay.getResult();
        throw new AutoException("还款计划失败，无法继续进行测试");
      }
      pay.queryClaimPayPlan(claimId);
      ClaimProcess process = new ClaimProcess();

      process.submitProcess(claimId,form);
      if(!process.status) {
        status = false;
        remark = process.remark;
       
      }else {
      
        status = true;
        remark = "[claimId="+claimId+",claimNum="+claimNo+",amount="+amount+"]";
      }  
     
	}

  @Override
  public boolean getStatus() {
    // TODO Auto-generated method stub
    return false;
  }
	
}
